package com.wys.proxydemo.jdk;

import com.wys.proxydemo.common.Person;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author godShan
 * @Description:
 * @date 2019/5/6 9:25
 */
public class JDKChuShi implements InvocationHandler {
    
    private Object target;

    public Object getInstance(Object obj) {
        target = obj;
        Class<?> clazz = obj.getClass();
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), clazz.getInterfaces(), this);
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        System.out.println("代理对象 : " + proxy);
        System.out.println("目标对象 : " + target);
        before();
        Object obj = method.invoke(target, args);
        after();
        return obj;
    }

    private void after() {
        System.out.println("客官您慢点，您的菜马上就到！");
    }

    private void before() {
        System.out.println("请问客官您需要点什么？小店所有菜系色香味俱全！");
    }
}
